package com.itheima.blog.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.blog.util.UserThreadLocalUtils;
import com.itheima.blog.web.domain.po.TAlbum;
import com.itheima.blog.web.service.ITAlbumService;
import com.itheima.common.domain.R;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import com.itheima.blog.web.service.ITAlbumService;
import lombok.RequiredArgsConstructor;

import com.itheima.blog.web.domain.po.TAlbum;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.itheima.blog.util.AliOssUtil;
import com.itheima.blog.util.UserThreadLocalUtils;
import com.itheima.blog.web.service.ITAlbumService;
import com.itheima.common.domain.PageReqDto;
import com.itheima.common.domain.PageResDto;
import com.itheima.common.domain.R;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 用户相册表 前端控制器
 * </p>
 *
 * @author hyf
 * @since 2023-07-12
 */
@RestController
@RequestMapping("/web/owner/album")
public class TAlbumController {
    @Autowired
    private ITAlbumService itAlbumService;


    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public R uploadFile(MultipartFile file) {
        AliOssUtil aliOssUtil = new AliOssUtil();
        if (file == null || file.getSize() <= 0) {
            return R.error("上传失败");
        }
        try {
            String upload = aliOssUtil.upload(file.getBytes(), UUID.randomUUID().toString());
            TAlbum tAlbum = new TAlbum();
            tAlbum.setUrl(upload);
            tAlbum.setStatus(1);
            tAlbum.setUpdateTime(LocalDateTime.now());
            tAlbum.setCreateTime(LocalDateTime.now());
            tAlbum.setUserId(UserThreadLocalUtils.getWebUserId());
            itAlbumService.save(tAlbum);
            return R.ok(upload);
        } catch (IOException e) {
            return R.error("上传失败");
        }
    }

    @DeleteMapping
    @ApiOperation("删除图片")
    public R rmPages(@RequestParam List<Long> ids) {
        itAlbumService.removeBatchByIds(ids);
        return R.ok();
    }

    @PostMapping
    @ApiOperation("修改图片")
    public R updatePage(MultipartFile file, @Param("id") Long id) {
        AliOssUtil aliOssUtil = new AliOssUtil();
        if (file == null || file.getSize() <= 0) {
            return R.error("上传失败");
        }
        try {
            String upload = aliOssUtil.upload(file.getBytes(), UUID.randomUUID().toString());
            TAlbum tAlbum = itAlbumService.getById(id);
            tAlbum.setUrl(upload);
            tAlbum.setUpdateTime(LocalDateTime.now());
            itAlbumService.updateById(tAlbum);
            return R.ok(upload);
        } catch (IOException e) {
            return R.error("修改失败");
        }
    }
    @PostMapping("/list")
    @ApiOperation("列表展示")
    public PageResDto<TAlbum> showPages(PageReqDto pageReqDto){
        return itAlbumService.showPages(pageReqDto);
    }

}

